package de.bk.timecalc.midlet.statemachine;

import java.util.Vector;

import de.bk.timecalc.midlet.Commands;
import de.bk.timecalc.midlet.TimeCalcMidlet;

public class StateMachine
{
  private TimeCalcMidlet midlet;
  private Vector allStates;
  private GuiStateGame game;

  public StateMachine(TimeCalcMidlet midlet)
  {
    this.midlet = midlet;
  }

  public void start()
  {
    this.allStates = new Vector();
    GuiStateStart start = new GuiStateStart(this);
    this.allStates.addElement(start);
    this.game = new GuiStateGame(this);
    this.allStates.addElement(this.game);
    GuiStateHelp help = new GuiStateHelp(this);
    this.allStates.addElement(help);
    GuiStateOptions options = new GuiStateOptions(this);
    this.allStates.addElement(options);

    start.addTransition(Commands.start, this.game);
    start.addTransition(Commands.help, help);
    start.addTransition(Commands.options, options);
    this.game.addTransition(Commands.exit, start);
    help.addTransition(Commands.exit, start);
    options.addTransition(Commands.exit, start);

    for (int i = 0; i < this.allStates.size(); i++)
    {
      ((AbstractGuiState) this.allStates.elementAt(i)).init();
    }
    start.enter();
  }

  public void destroy()
  {
    for (int i = 0; i < this.allStates.size(); i++)
    {
      ((AbstractGuiState) this.allStates.elementAt(i)).destroy();
    }
  }

  TimeCalcMidlet getMidlet()
  {
    return this.midlet;
  }

  GuiStateGame getGame()
  {
    return this.game;
  }
}
